package com.aliothservice.catfish.product.data;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.query.Update;

public class Vehicle {
	
	public static Update updateFromVehiclePending(VehiclePending pending) {
		Update update = new Update();
		
		update.set("userId", pending.getUserId());
		update.set("licensePlate", pending.getLicensePlate());
		update.set("province", pending.getProvince());
		update.set("city", pending.getCity());
		update.set("purchaseDate", pending.getPurchaseDate());
		update.set("fullLoadWeight", pending.getFullLoadWeight());
		update.set("fullLoadMass", pending.getFullLoadMass());
		update.set("vehiclePhotoId", pending.getVehiclePhotoId());
		update.set("vehicleInsidePhotoId", pending.getVehicleInsidePhotoId());
		update.set("temperatureMin", pending.getTemperatureMin());
		update.set("temperatureMax", pending.getTemperatureMax());
		update.set("lengthDef", pending.getLengthDef());
		update.set("owner", pending.getOwner());
		update.set("odometer", pending.getOdometer());
		update.set("transportationLicenseImageId", pending.getTransportationLicenseImageId());
		update.set("insuranceImageId", pending.getInsuranceImageId());
		update.set("manInChargeName", pending.getManInChargeName());
		update.set("manInChargePhoneNumber", pending.getManInChargePhoneNumber());
		update.set("lastUpdateTime", new Date());
		if (null != pending.getExpiredBy()) {
			update.set("expiredBy", pending.getExpiredBy());
		}
		if (null != pending.getDescriptionOfReject()) {
			update.set("descriptionOfReject", pending.getDescriptionOfReject());
		}
		
		return update;
	}
	
	public Map<String, String> toMap() {
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("id", this.getId());
		map.put("userId", this.getUserId());
		map.put("licensePlate", this.getLicensePlate());
		map.put("province", this.getProvince());
		map.put("city", this.getCity());
		if (null != this.getDistrict()) {
			map.put("district", this.getDistrict());
		}
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		map.put("purchaseDate", formatter.format(this.getPurchaseDate()));
		map.put("fullLoadWeight", this.getFullLoadWeight().toString());
		map.put("fullLoadMass", this.getFullLoadMass().toString());
		map.put("vehiclePhotoId", this.getVehiclePhotoId());
		map.put("vehicleInsidePhotoId", this.getVehicleInsidePhotoId());
		map.put("temperatureMin", this.getTemperatureMin().toString());
		map.put("temperatureMax", this.getTemperatureMax().toString());
		map.put("lengthDef", this.getLengthDef());
		map.put("owner", this.getOwner());
		map.put("odometer", this.getOdometer().toString());
		map.put("transportationLicenseImageId", this.getTransportationLicenseImageId());
		map.put("insuranceImageId", this.getInsuranceImageId());
		map.put("manInChargeName", this.getManInChargeName());
		map.put("manInChargePhoneNumber", this.getManInChargePhoneNumber());
		DateFormat fullFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		map.put("lastUpdateTime", fullFormatter.format(this.getLastUpdateTime()));
		if (null != this.getExpiredBy()) {
			map.put("expiredBy", formatter.format(this.getExpiredBy()));
		}
		if (null != this.getDescriptionOfReject()) {
			map.put("descriptionOfReject", this.getDescriptionOfReject());
		}
		if (null != this.getDeviceSN()) {
			map.put("deviceSN", this.getDeviceSN());
		}
		
		return map;
	}
	
	public Map<String, Object> toSearchableMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("id", this.getId());
		map.put("licensePlate", this.getLicensePlate());
		map.put("province", this.getProvince());
		map.put("city", this.getCity());
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		map.put("purchaseDate", formatter.format(this.getPurchaseDate()));
		map.put("odometer", this.getOdometer());
		map.put("temperatureMin", this.getTemperatureMin());
		map.put("temperatureMax", this.getTemperatureMax());
		map.put("vehiclePhotoId", this.getVehiclePhotoId());
		map.put("vehicleInsidePhotoId", this.getVehicleInsidePhotoId());
		map.put("transportationLicenseImageId", this.getTransportationLicenseImageId());
		map.put("manInChargeName", this.getManInChargeName());
		if (this.getManInChargePhoneNumber().length() != 11) {
			map.put("manInChargePhoneNumber", this.getManInChargePhoneNumber());
		} else {
			String maskedPhoneNumber = String.format("%s*****%s", this.getManInChargePhoneNumber().substring(0, 2), this.getManInChargePhoneNumber().substring(7, 10));
			map.put("manInChargePhoneNumber", maskedPhoneNumber);
		}
		if (null != this.getDeviceSN()) {
			map.put("deviceSN", this.getDeviceSN());
		}
		
		return map;
	}
	
	@Id
	private String id;
	
	// who owns this vehicle
	private String userId;
	
	// cannot be modified once approved
	private String licensePlate;
	private String province;
	private String city;
	private String district;
	private Date purchaseDate;
	private Integer fullLoadWeight;
	private Integer fullLoadMass;
	private String vehiclePhotoId; // stored in GridFS
	private String vehicleInsidePhotoId; // stored in GridFS
	private Integer temperatureMin;
	private Integer temperatureMax;
	// links to VehicleLengthDef.identifier
	private String lengthDef;
	
	private Date lastUpdateTime;
	
	// set by administrator, checking triggered from the backend
	private Date expiredBy;

	private String owner;
	private Integer odometer;
	private String transportationLicenseImageId; // stored in GridFS
	private String insuranceImageId; // stored in GridFS
	
	private String manInChargeName;
	private String manInChargePhoneNumber;
	
	private String status;
	private String descriptionOfReject;
	
	private String individual;
	private String enterprise;
	
	private String deviceSN;
	
	public static final String VEHICLE_STATUS_APPROVED = "approved";
	public static final String VEHICLE_STATUS_REJECTED = "rejected";

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getLicensePlate() {
		return licensePlate;
	}

	public void setLicensePlate(String licensePlate) {
		this.licensePlate = licensePlate;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Integer getTemperatureMin() {
		return temperatureMin;
	}

	public void setTemperatureMin(Integer temperatureMin) {
		this.temperatureMin = temperatureMin;
	}

	public Integer getTemperatureMax() {
		return temperatureMax;
	}

	public void setTemperatureMax(Integer temperatureMax) {
		this.temperatureMax = temperatureMax;
	}

	public Integer getFullLoadWeight() {
		return fullLoadWeight;
	}

	public void setFullLoadWeight(Integer fullLoadWeight) {
		this.fullLoadWeight = fullLoadWeight;
	}

	public Integer getFullLoadMass() {
		return fullLoadMass;
	}

	public void setFullLoadMass(Integer fullLoadMass) {
		this.fullLoadMass = fullLoadMass;
	}

	public Date getExpiredBy() {
		return expiredBy;
	}

	public void setExpiredBy(Date expiredBy) {
		this.expiredBy = expiredBy;
	}

	public Date getPurchaseDate() {
		return purchaseDate;
	}

	public void setPurchaseDate(Date purchaseDate) {
		this.purchaseDate = purchaseDate;
	}

	public String getVehiclePhotoId() {
		return vehiclePhotoId;
	}

	public void setVehiclePhotoId(String vehiclePhotoId) {
		this.vehiclePhotoId = vehiclePhotoId;
	}

	public String getLengthDef() {
		return lengthDef;
	}

	public void setLengthDef(String lengthDef) {
		this.lengthDef = lengthDef;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public Integer getOdometer() {
		return odometer;
	}

	public void setOdometer(Integer odometer) {
		this.odometer = odometer;
	}

	public String getTransportationLicenseImageId() {
		return transportationLicenseImageId;
	}

	public void setTransportationLicenseImageId(String transportationLicenseImageId) {
		this.transportationLicenseImageId = transportationLicenseImageId;
	}

	public String getInsuranceImageId() {
		return insuranceImageId;
	}

	public void setInsuranceImageId(String insuranceImageId) {
		this.insuranceImageId = insuranceImageId;
	}

	public String getDescriptionOfReject() {
		return descriptionOfReject;
	}

	public void setDescriptionOfReject(String descriptionOfReject) {
		this.descriptionOfReject = descriptionOfReject;
	}

	public String getManInChargeName() {
		return manInChargeName;
	}

	public void setManInChargeName(String manInChargeName) {
		this.manInChargeName = manInChargeName;
	}

	public String getManInChargePhoneNumber() {
		return manInChargePhoneNumber;
	}

	public void setManInChargePhoneNumber(String manInChargePhoneNumber) {
		this.manInChargePhoneNumber = manInChargePhoneNumber;
	}

	public String getVehicleInsidePhotoId() {
		return vehicleInsidePhotoId;
	}

	public void setVehicleInsidePhotoId(String vehicleInsidePhotoId) {
		this.vehicleInsidePhotoId = vehicleInsidePhotoId;
	}

	public String getIndividual() {
		return individual;
	}

	public void setIndividual(String individual) {
		this.individual = individual;
	}

	public String getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(String enterprise) {
		this.enterprise = enterprise;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getDeviceSN() {
		return deviceSN;
	}

	public void setDeviceSN(String deviceSN) {
		this.deviceSN = deviceSN;
	}

}
